﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace finalexam
{
    public partial class WebForm12 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "欢迎来到学生信息管理系统";
        }

        protected void btnRead_Click(object sender, EventArgs e)
        {

            string strCn = "server=pc.hstc.cn;database=dhd2014112134;uid=2014112134;pwd=19951124";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                cn.Open();

                this.Label13.Text = "";
                string strSql = "SELECT   Cname,Name, Grade FROM      Grade3  WHERE   (Sem = N'" + this.DdListSem.Text + "') AND (No = N'" + this.txtNo.Text + "') AND (Cno = N'" + this.DdListCno.Text + "')";
                SqlCommand cmd = new SqlCommand(strSql, cn);
                SqlDataReader dr = cmd.ExecuteReader();

                if (dr.Read())
                {
                    this.txtGrade.Text = dr["Grade"].ToString();
                    this.txtName.Text = dr["Name"].ToString();
                    this.txtCname.Text = dr["Cname"].ToString();
                    
                }
                else
                {
                    this.Label13.Text = "该学生成绩信息不存在！或查询信息不完善！";

                    this.txtGrade.Text = "";

                }

                dr.Close();
            }
            catch (Exception ex)
            {
                this.Label13.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        protected void btnRead1_Click(object sender, EventArgs e)
        {
            String strCn = "server=pc.hstc.cn;database=dhd2014112134;uid=2014112134;pwd=19951124";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                cn.Open();

                this.Label13.Text = "";

                string strSql = "SELECT    Name, Cno , Cname, Grade  FROM      Grade3 WHERE   (No = N'" + this.txtNo.Text + "')AND(Sem = N'" + this.DdListSem.Text + "') ";
                SqlCommand cmd = new SqlCommand(strSql, cn);
                SqlDataReader dr = cmd.ExecuteReader();
                this.GridView2.DataSource = dr;
                this.GridView2.DataBind();
                dr.Close();

            }
            catch (Exception ex)
            {
                this.Label13.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string strCn = "server=pc.hstc.cn;database=dhd2014112134;uid=2014112134;pwd=19951124";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                cn.Open();

                this.Label13.Text = "";

                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("UPDATE  Grade3 ");
                SqlStringBuilder.AppendFormat("SET Name = N'{0}',Cname = N'{1}',Grade = {2}", this.txtName.Text, this.txtCname.Text, this.txtGrade.Text);

                //SqlStringBuilder.AppendFormat("WHERE (Sem = N'{0}') AND（No = N'{1}'）AND（Cno = N'{2}') ", this.txtSem.Text, this.txtNo.Text, this.txtCno.Text);//待定
                SqlStringBuilder.AppendFormat("WHERE   (Sem = N'{0}')AND(No = N'{1}')AND(Cno = N'{2}') ", this.DdListSem.Text, this.txtNo.Text, this.DdListCno.Text);
                string strSql = SqlStringBuilder.ToString();
                SqlCommand cmd = new SqlCommand(strSql, cn);
                int iUpd = cmd.ExecuteNonQuery();
                if (iUpd < 1)
                {
                    this.Label13.Text = "更新失败！";
                }
                else
                {
                    this.Label13.Text = "更新" + iUpd.ToString() + "条记录！";
                }
            }
            catch (Exception ex)
            {
                this.Label13.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            string strCn = "server=pc.hstc.cn;database=dhd2014112134;uid=2014112134;pwd=19951124";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                cn.Open();

                this.Label13.Text = "";

                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("INSERT INTO Grade3 ");
                SqlStringBuilder.Append("                (Sem, No,Name, Cno, Cname,Grade) ");
                SqlStringBuilder.Append("VALUES   (@Sem,@No,@Name,@Cno,@Cname,@Grade) ");

                string strSql = SqlStringBuilder.ToString();
                SqlCommand cmd = new SqlCommand(strSql, cn);
                SqlParameter pmsNo = new SqlParameter("@No", this.txtNo.Text);
                SqlParameter pmsName = new SqlParameter("@Name", this.txtName.Text);
                SqlParameter pmsSem = new SqlParameter("@Sem", this.DdListSem.Text);
                SqlParameter pmsCno = new SqlParameter("@Cno", this.DdListCno.Text);
                SqlParameter pmsCname = new SqlParameter("@Cname", this.txtCname.Text);
                SqlParameter pmsGrade = new SqlParameter("@Grade", this.txtGrade.Text);

                cmd.Parameters.Add(pmsNo);
                cmd.Parameters.Add(pmsName);
                cmd.Parameters.Add(pmsSem);
                cmd.Parameters.Add(pmsCno);
                cmd.Parameters.Add(pmsCname);
                cmd.Parameters.Add(pmsGrade);

                int iUpd = cmd.ExecuteNonQuery();
                if (iUpd < 1)
                {
                    this.Label13.Text = "新增失败！";
                }
                else
                {
                    this.Label13.Text = "新增" + iUpd.ToString() + "条记录！";
                }
            }
            catch (Exception ex)
            {
                this.Label13.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string strCn = "server=pc.hstc.cn;database=dhd2014112134;uid=2014112134;pwd=19951124";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                cn.Open();

                this.Label13.Text = "";

                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("DELETE FROM Grade3 ");

                SqlStringBuilder.AppendFormat("WHERE   (Sem = N'{0}')AND(No = N'{1}')AND(Cno = N'{2}') ", this.DdListSem.Text, this.txtNo.Text, this.DdListCno.Text);

                string strSql = SqlStringBuilder.ToString();
                SqlCommand cmd = new SqlCommand(strSql, cn);

                int iUpd = cmd.ExecuteNonQuery();
                if (iUpd < 1)
                {
                    this.Label13.Text = "删除失败！";
                }
                else
                {
                    this.Label13.Text = "删除" + iUpd.ToString() + "条记录！";
                    this.DdListSem.Text = "";
                    this.DdListCno.Text = "";
                    this.txtCname.Text = "";
                    this.txtName.Text = "";
                    this.txtNo.Text = "";
                    this.txtGrade.Text = "";
                }
            }
            catch (Exception ex)
            {
                this.Label13.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["Theme"] = this.DropDownList1.SelectedValue.ToString();
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["Theme"] != null)
                this.Theme = Session["Theme"].ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }
    }
}
        
   